package com.yuehuanghun.admin.quartz.controller;

import java.util.List;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.util.UserUtils;
import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.quartz.config.exception.TaskException;
import com.yuehuanghun.admin.quartz.domain.SysJob;
import com.yuehuanghun.admin.quartz.service.ISysJobService;
import com.yuehuanghun.admin.quartz.util.CronUtils;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 调度任务信息操作处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController extends BaseResource {
	@Autowired
	private ISysJobService jobService;

	/**
	 * 查询定时任务列表
	 */
	@SaCheckPermission("monitor:job:list")
	@GetMapping("/list")
	public PageDTO<SysJob> list(SysJob sysJob) {
		List<SysJob> list = jobService.getByExample(sysJob, getPage());
		return toPage(list);
	}

	/**
	 * 导出定时任务列表
	 */
	@Log(title = "定时任务", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("monitor:job:export")
	public String export(SysJob sysJob) {
		List<SysJob> list = jobService.getByExample(sysJob);
		ExcelUtil<SysJob> util = new ExcelUtil<>(SysJob.class);
		return util.exportExcel(list, "定时任务");
	}

	/**
	 * 获取定时任务详细信息
	 */
	@GetMapping(value = "/{jobId}")
	@SaCheckPermission("monitor:job:query")
	public SysJob getInfo(@PathVariable("jobId") Long jobId) {
		return jobService.getById(jobId).orElse(null);
	}

	/**
	 * 新增定时任务
	 */
	@Log(title = "定时任务", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("monitor:job:add")
	public void add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
		if (!CronUtils.isValid(sysJob.getCronExpression())) {
			throw new ValidateException("cron表达式不正确");
		}
		sysJob.setCreateBy(UserUtils.getCurrentUserName());
		jobService.insertJob(sysJob);
	}

	/**
	 * 修改定时任务
	 */
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("monitor:job:edit")
	public void edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
		if (!CronUtils.isValid(sysJob.getCronExpression())) {
			throw new ValidateException("cron表达式不正确");
		}
		sysJob.setUpdateBy(UserUtils.getCurrentUserName());
		jobService.updateJob(sysJob);
	}

	/**
	 * 定时任务状态修改
	 */
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	@SaCheckPermission("monitor:job:changeStatus")
	public void changeStatus(@RequestBody SysJob job) throws SchedulerException {
		SysJob newJob = jobService.getById(job.getJobId()).orElseThrow(() -> new ValidateException("定义任务不存在"));
		newJob.setStatus(job.getStatus());
		jobService.changeStatus(newJob);
	}

	/**
	 * 定时任务立即执行一次
	 */
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping("/run")
	@SaCheckPermission("monitor:job:changeStatus")
	public void run(@RequestBody SysJob job) throws SchedulerException {
		jobService.run(job);
	}

	/**
	 * 删除定时任务
	 */
	@Log(title = "定时任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jobIds}")
	@SaCheckPermission("monitor:job:remove")
	public void remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
		jobService.deleteJobByIds(jobIds);
	}
}
